package com.mariusz.panels;

import java.awt.Color;

import javax.swing.JScrollPane;
import javax.xml.crypto.Data;

import org.jdesktop.swingx.JXTitledPanel;

import com.mariusz.data.dto.Property;
import com.mariusz.data.dto.Room;
import com.mariusz.managers.DataManager;
import com.mariusz.managers.NavigationManager;
import com.mariusz.managers.NavigationManager.Screen;
import com.mariusz.managers.PropertyManager;

import org.jdesktop.swingx.JXImageView;
import org.jdesktop.swingx.JXLabel;
import org.jdesktop.swingx.JXTextField;
import org.jdesktop.swingx.JXList;
import org.jdesktop.swingx.JXButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import org.jdesktop.swingx.JXImagePanel;
import java.awt.Toolkit;
import java.util.List;
import javax.swing.JLabel;

public class PropertyEditPanel extends JXTitledPanel implements ActionListener{

	private JXTextField propertyName;
	private JXTextField propertyAddress;
	private JXList roomList;
	private JXButton btnAddRoom;
	private JXTextField newRoomName;
	private JXButton btnRemoveRoom ; 
	private JXButton btnCancel;
	private JXButton btnSave;
	private JLabel lblRoomsError;
	private JLabel lblPropertyAddressError;
	private JLabel lblPropertyNameError; 
	
	public PropertyEditPanel() {
		getContentContainer().setBackground(Color.WHITE);
		setTitleForeground(new Color(0, 0, 0));
		setTitle("Property details");
		getContentContainer().setBounds(10, 10, 100, 100);
		getContentContainer().setLayout(null);
		
		btnAddRoom = new JXButton();
		btnAddRoom.addActionListener(this);
		btnAddRoom.setText("Add Room");
		btnAddRoom.setBounds(488, 248, 98, 28);
		getContentContainer().add(btnAddRoom);
		
		JXLabel lblPropertyName = new JXLabel();
		lblPropertyName.setText("Property Name:");
		lblPropertyName.setBounds(303, 21, 98, 20);
		getContentContainer().add(lblPropertyName);
		
		propertyName = new JXTextField();
		propertyName.setBounds(303, 36, 267, 28);
		getContentContainer().add(propertyName);
		
		JXLabel lblPropertyAddress = new JXLabel();
		lblPropertyAddress.setText("Property Address:");
		lblPropertyAddress.setBounds(303, 114, 112, 20);
		getContentContainer().add(lblPropertyAddress);
		
		propertyAddress = new JXTextField();
		propertyAddress.setBounds(303, 134, 267, 28);
		getContentContainer().add(propertyAddress);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 248, 338, 147);
		getContentContainer().add(scrollPane);
		
		roomList = new JXList();
		scrollPane.setViewportView(roomList);
		
		JXLabel lblNewRoomName = new JXLabel();
		lblNewRoomName.setText("New Room Name:");
		lblNewRoomName.setBounds(357, 225, 112, 20);
		getContentContainer().add(lblNewRoomName);
		
		newRoomName = new JXTextField();
		newRoomName.setBounds(357, 248, 121, 28);
		getContentContainer().add(newRoomName);
		
		btnRemoveRoom = new JXButton();
		btnRemoveRoom.addActionListener(this);
		btnRemoveRoom.setText("Remove Room");
		btnRemoveRoom.setBounds(358, 293, 112, 28);
		getContentContainer().add(btnRemoveRoom);
		
		ResourceImagePanel imagePanel = new ResourceImagePanel("House Layout.jpg");
		imagePanel.setBounds(10, 10, 283, 213);
		getContentContainer().add(imagePanel);		
		
		btnCancel = new JXButton();
		btnCancel.setText("Cancel");
		btnCancel.setBounds(409, 406, 112, 28);
		getContentContainer().add(btnCancel);
		btnCancel.addActionListener(this);
		
		btnSave = new JXButton();
		btnSave.setText("Save Property");
		btnSave.setBounds(534, 406, 112, 28);
		getContentContainer().add(btnSave);
		lblPropertyNameError = new JLabel("");
		lblPropertyNameError.setForeground(Color.RED);
		lblPropertyNameError.setBounds(303, 160, 338, 20);
		getContentContainer().add(lblPropertyNameError);
		lblPropertyAddressError = new JLabel("");
		lblPropertyAddressError.setForeground(Color.RED);
		lblPropertyAddressError.setBounds(303, 62, 343, 20);
		getContentContainer().add(lblPropertyAddressError);
		
		lblRoomsError = new JLabel("");
		lblRoomsError.setForeground(Color.RED);
		lblRoomsError.setBounds(10, 395, 338, 20);
		getContentContainer().add(lblRoomsError);
		
		JXLabel lblRooms = new JXLabel();
		lblRooms.setText("Rooms inside:");
		lblRooms.setBounds(10, 225, 239, 20);
		getContentContainer().add(lblRooms);
		btnSave.addActionListener(this);
		
	}

	public void editProperty(Property property) {
		propertyName.setText(property.getName());
		propertyAddress.setText(property.getAddress());
			
		refreshRoomsTable(property);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnAddRoom ){
			Property property = DataManager.getInstance().getCurrentProperty();
			for(Room room : property.getRooms()){
				if(room.getName().equalsIgnoreCase(newRoomName.getText())){
					lblRoomsError.setText("This room allready exists");
					return;
				}
			}
			Room room = new Room();
			room.setName(newRoomName.getText());
			property.getRooms().add(room);
			
			//refresh
			refreshRoomsTable(property);
			newRoomName.setText("");
			
		}else if (e.getSource()== btnRemoveRoom){ //TODO Check it.
			Property property = DataManager.getInstance().getCurrentProperty();
			Object selectedRoom = roomList.getSelectedValue();
			if(selectedRoom != null){
			for(Room room : property.getRooms()){
				if(room.getName().equals(selectedRoom.toString())){
					property.getRooms().remove(room);
					break;
				}
			}
			//refresh
			refreshRoomsTable(property);
			}
		}else if(e.getSource() == btnCancel){
			DataManager.getInstance().setCurrentProperty(null);
			NavigationManager.getInstance().switchTo(Screen.PROPERTIES);
		}else if(e.getSource() == btnSave){
			if(validateForm()){
			Property property = DataManager.getInstance().getCurrentProperty();
			if(property != null){
				fillDTOFields(property);
				//TODO add rooms...
				List<Property> list = DataManager.getInstance().getProperties();
				if(!list.contains(property)){
					list.add(property);
				}
			}
			NavigationManager.getInstance().switchTo(Screen.PROPERTIES);
			}
		}
	}
	
	private boolean validateForm(){
		boolean passed = true;
		if(propertyName.getText().isEmpty()){
			lblPropertyNameError.setText("Please fill out the name of this Property");
			passed = false;
		}
		if(propertyAddress.getText().isEmpty()){
			lblPropertyAddressError.setText("Please fill out the address of this Property");
			passed =false;
		}
		if(roomList.getModel().getSize() <= 0){
			lblRoomsError.setText("Please add at least one room to this Property");
			passed = false;
		}
		
		if(passed){
			lblPropertyNameError.setText("");
			lblPropertyAddressError.setText("");
			lblRoomsError.setText("");
		}
		
		return passed;
	}
	
	private void fillDTOFields(Property property) {
		property.setAddress(propertyAddress.getText());
		property.setName(propertyName.getText());
		refreshRoomsTable(property);
	}

	@Override
	public void setVisible(boolean aFlag) {
		fillUIFields();
		super.setVisible(aFlag);
	}
	
	private void fillUIFields() {
		Property property = DataManager.getInstance().getCurrentProperty();
		if(property != null){
			propertyName.setText(property.getName());
			propertyAddress.setText(property.getAddress());
			refreshRoomsTable(property);
		}
		
	}

	private void refreshRoomsTable(Property property){
		roomList.setModel(DataManager.getInstance().toRoomsListModel(property.getRooms()));
		
	}
}
